<template>
  <div v-loading.fullscreen.lock="fullscreenLoading">
    <el-form :model="courseMarketForm" label-width="110px" :rules="courseMarketFormRules" ref="courseMarketForm"
             v-loading="courseMarketFormLoading">
      <el-form-item label="课程价格" prop="charge" v-loading="expirationDateLoading">
        <b v-for="charge in chargeList">
          <el-radio v-model="courseMarketForm.charge" :label="charge.sdId">{{charge.sdName}}</el-radio>
        </b>
        <br/>
        金额（元）：
        <el-input :disabled="this.courseMarketForm.charge == '203002'?false:true"
                  v-model="courseMarketForm.price"></el-input>
      </el-form-item>
      <el-form-item label="课程有效期" prop="valid" v-loading="chargingRulesLoading">
        <b v-for="valid in validList">
          <el-radio v-model="courseMarketForm.valid" :label="valid.sdId">{{valid.sdName}}</el-radio>&nbsp;&nbsp;
        </b>
        <br/>
        开始时间：
        <el-date-picker :disabled="this.courseMarketForm.valid == '204002'?false:true" type="datetime"
                        placeholder="选择日期" v-model="courseMarketForm.startTime"
                        value-format="yyyy-MM-dd hh:mm:ss"></el-date-picker>
        结束时间：
        <el-date-picker :disabled="this.courseMarketForm.valid == '204002'?false:true" type="datetime"
                        placeholder="选择日期" v-model="courseMarketForm.endTime" value-format="yyyy-MM-dd hh:mm:ss"
                        ></el-date-picker>
      </el-form-item>
      <el-form-item label="服务咨询QQ" prop="qq">
        <el-input v-model="courseMarketForm.qq"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click.native="save">提交</el-button>
      </el-form-item>
    </el-form>

  </div>
</template>
<script>
  import * as courseApi from '../../api/course';
  import * as systemApi from '../../../../base/api/system';

  export default {
    data() {
      return {
        expirationDateLoading: true,
        chargingRulesLoading: true,
        courseMarketFormLoading: true,
        fullscreenLoading: false,
        active: 1,
        dotype: '',
        courseid: '',
        chargeList: [],
        validList: [],
        price_tag: false,
        expires_tag: false,
        courseMarketForm: {
          id: '',
          charge: '',
          valid: '',
          price: '',
          expires: '',
          startTime: '',
          endTime: '',
          users: '',
          expiration: [],
          courseid: this.courseid
        },
        courseMarketFormRules: {
          charge: [
            {required: true, message: '请选择收费规则', trigger: 'blur'}
          ],
          valid: [
            {required: true, message: '请选择有效期', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      // 提交按钮
      save: function () {
        this.$refs.courseMarketForm.validate((valid) => {
          if (valid) {
            // 校验选择了收费课程 没有输入金额的情况
            if (this.courseMarketForm.charge === "203002" && (this.courseMarketForm.price === null || this.courseMarketForm.price === "" || this.courseMarketForm.price <= 0)) {
              this.$message.error('请输入课程收费金额');
              return false;
            }
            // 校验选择了指定时间 没有选择开始时间和收费时间的情况
            if (this.courseMarketForm.valid === "204002" && (this.courseMarketForm.startTime === null || this.courseMarketForm.startTime === "" || this.courseMarketForm.endTime === null || this.courseMarketForm.endTime === "")) {
              this.$message.error('请选择课程的开始时间和结束时间');
              return false;
            }
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.fullscreenLoading = true;
              // 转换时间格式
              let startTime = this.$moment(this.courseMarketForm.startTime).format("YYYY-MM-DD HH:mm:ss")
              let endTime = this.$moment(this.courseMarketForm.endTime).format("YYYY-MM-DD HH:mm:ss")
              this.courseMarketForm.startTime = startTime;
              this.courseMarketForm.endTime = endTime;
              // 判断结束时间是否大于当前时间
              if (this.courseMarketForm.valid==="204002"){
                if (startTime >= endTime){
                  this.$message.error('请选择正确的结束时间');
                  this.courseMarketForm.endTime = ""
                  this.fullscreenLoading = false;
                  return false
                }
              }else {
                // 如果选择了永久有效可能出现传输到后台的数据是 Invalid date字符串,需要重置下时间
                //startTime: "Invalid date"
                // endTime: "Invalid date"
                this.courseMarketForm.startTime = ""
                this.courseMarketForm.endTime = ""
              }
              courseApi.updateCourseMarket(this.courseid, this.courseMarketForm).then((res) => {
                if (res.success) {
                  this.fullscreenLoading = false;
                  this.$message.success('提交成功');
                  this.getCourseMarket();
                  if (this.dotype == '1') {
                    //跳转到课程图片
                    this.$router.push({path: '/course/add/plan/3/1/' + this.courseid})
                  }
                } else {
                  this.fullscreenLoading = false;
                  this.$message.error(res.message);
                }
              });
            }).catch(() => {
              this.$message.error('未知错误');
            });
          }
        });
      },
      //获取课程营销信息
      getCourseMarket() {
        courseApi.getCourseMarketById(this.courseid).then((res) => {
          if (res) {
            this.courseMarketForm = res;
            if (this.expirationDateLoading === false && this.chargingRulesLoading === false) {
              this.courseMarketFormLoading = false;
            }
          }
        }).catch(()=>{
          this.$message.error('未知错误');
        });
      },

    },
    mounted() {
      //操作类型
      this.dotype = this.$route.params.dotype;
      //课程id
      this.courseid = this.$route.params.courseid;

      this.courseMarketForm.id = this.courseid;
      //查询字典
      // 查询课程收费性
      systemApi.sys_getDictionary('203').then((res) => {
        this.chargeList = res.dvalue;
        this.expirationDateLoading = false;
        if (this.chargingRulesLoading === false) {
          this.courseMarketFormLoading = false;
        }
      });
      // 查询课程有效性
      systemApi.sys_getDictionary('204').then((res) => {
        this.validList = res.dvalue;
        this.chargingRulesLoading = false;
        if (this.expirationDateLoading === false) {
          this.courseMarketFormLoading = false;
        }
      });
      this.getCourseMarket();
    }
  }
</script>
<style scoped>


</style>
